'''

'''


intercept_script = """
    window.__apiResponses = [];

    // 拦截 fetch API
    const originalFetch = window.fetch;
    window.fetch = function(...args) {
        const [url, options = {}] = args;
        const method = options.method || 'GET';
        const body = options.body || '';

        return originalFetch.apply(this, args).then(response => {
            const clonedResponse = response.clone();
            clonedResponse.text().then(text => {
                window.__apiResponses.push({
                    url: url,
                    method: method,
                    status: response.status,
                    requestBody: body,
                    response: text,
                    timestamp: new Date().toISOString(),
                    type: 'fetch'
                });
            });
            return response;
        });
    };

    // 拦截 XMLHttpRequest
    const originalXHR = window.XMLHttpRequest;
    window.XMLHttpRequest = function() {
        const xhr = new originalXHR();
        const originalOpen = xhr.open;
        const originalSend = xhr.send;

        let requestUrl = '';
        let requestMethod = '';
        let requestBody = '';

        xhr.open = function(method, url) {
            requestMethod = method;
            requestUrl = url;
            return originalOpen.apply(this, arguments);
        };

        xhr.send = function(body) {
            requestBody = body;

            this.addEventListener('load', function() {
                window.__apiResponses.push({
                    url: requestUrl,
                    method: requestMethod,
                    status: this.status,
                    requestBody: requestBody,
                    response: this.responseText,
                    timestamp: new Date().toISOString(),
                    type: 'xhr'
                });
            });

            return originalSend.apply(this, arguments);
        };

        return xhr;
    };
    """

intercept_script_url_only = """
    window.__apiUrls = [];

    // 拦截 fetch API
    const originalFetch = window.fetch;
    window.fetch = function(...args) {
        const url = args[0];

        return originalFetch.apply(this, args).then(response => {
            window.__apiUrls.push(url);
            return response;
        });
    };

    // 拦截 XMLHttpRequest
    const originalXHR = window.XMLHttpRequest;
    window.XMLHttpRequest = function() {
        const xhr = new originalXHR();
        const originalOpen = xhr.open;

        xhr.open = function(method, url) {
            window.__apiUrls.push(url);
            return originalOpen.apply(this, arguments);
        };

        return xhr;
    };

    // 返回所有捕获的URL
    return window.__apiUrls;
"""

challenge_js = """
    function extractCaptchaParams() {
        var gokuProps = window.gokuProps;
        var websiteKey = gokuProps ? gokuProps.key : "未找到";
        var context = gokuProps ? gokuProps.context : "未找到";
        var iv = gokuProps ? gokuProps.iv : "未找到";
        var awsWafCookieDomainList = window.awsWafCookieDomainList || [];

        var scripts = Array.from(document.querySelectorAll('script[src]'));
        var challengeScriptUrl = scripts.find(script => script.src && script.src.includes('challenge.js'))?.src || "未找到";
        var captchaScriptUrl = scripts.find(script => script.src && script.src.includes('captcha.js'))?.src || "未找到";

        return {
            websiteKey: websiteKey,
            context: context,
            iv: iv,
            awsWafCookieDomainList: awsWafCookieDomainList,
            challengeScriptUrl: challengeScriptUrl,
            captchaScriptUrl: captchaScriptUrl,
            gokuProps: gokuProps
        };
    }
    return extractCaptchaParams();
    """


intercept_and_captcha_script = """
    // 第一部分：API 拦截功能
    window.__apiResponses = [];

    // 拦截 fetch API
    const originalFetch = window.fetch;
    window.fetch = function(...args) {
        const [url, options = {}] = args;
        const method = options.method || 'GET';
        const body = options.body || '';

        return originalFetch.apply(this, args).then(response => {
            const clonedResponse = response.clone();
            clonedResponse.text().then(text => {
                window.__apiResponses.push({
                    url: url,
                    method: method,
                    status: response.status,
                    requestBody: body,
                    response: text,
                    timestamp: new Date().toISOString(),
                    type: 'fetch'
                });
            });
            return response;
        });
    };

    // 拦截 XMLHttpRequest
    const originalXHR = window.XMLHttpRequest;
    window.XMLHttpRequest = function() {
        const xhr = new originalXHR();
        const originalOpen = xhr.open;
        const originalSend = xhr.send;

        let requestUrl = '';
        let requestMethod = '';
        let requestBody = '';

        xhr.open = function(method, url) {
            requestMethod = method;
            requestUrl = url;
            return originalOpen.apply(this, arguments);
        };

        xhr.send = function(body) {
            requestBody = body;

            this.addEventListener('load', function() {
                window.__apiResponses.push({
                    url: requestUrl,
                    method: requestMethod,
                    status: this.status,
                    requestBody: requestBody,
                    response: this.responseText,
                    timestamp: new Date().toISOString(),
                    type: 'xhr'
                });
            });

            return originalSend.apply(this, arguments);
        };

        return xhr;
    };

    // 第二部分：验证码参数提取功能
    function extractCaptchaParams() {
        var gokuProps = window.gokuProps;
        var websiteKey = gokuProps ? gokuProps.key : "未找到";
        var context = gokuProps ? gokuProps.context : "未找到";
        var iv = gokuProps ? gokuProps.iv : "未找到";
        var awsWafCookieDomainList = window.awsWafCookieDomainList || [];

        var scripts = Array.from(document.querySelectorAll('script[src]'));
        var challengeScriptUrl = scripts.find(script => script.src && script.src.includes('challenge.js'))?.src || "未找到";
        var captchaScriptUrl = scripts.find(script => script.src && script.src.includes('captcha.js'))?.src || "未找到";

        return {
            websiteKey: websiteKey,
            context: context,
            iv: iv,
            awsWafCookieDomainList: awsWafCookieDomainList,
            challengeScriptUrl: challengeScriptUrl,
            captchaScriptUrl: captchaScriptUrl,
            gokuProps: gokuProps,
            // 添加拦截的数据
            interceptedApiResponses: window.__apiResponses
        };
    }

    // 返回合并的结果
    return extractCaptchaParams();
"""

intercept_script_with_delay = """
    return new Promise((resolve) => {
        window.__apiUrls = new Set();

        // 拦截 fetch API
        const originalFetch = window.fetch;
        window.fetch = function(...args) {
            const url = args[0];
            window.__apiUrls.add(url);
            return originalFetch.apply(this, args);
        };

        // 拦截 XMLHttpRequest
        const originalXHR = window.XMLHttpRequest;
        window.XMLHttpRequest = function() {
            const xhr = new originalXHR();
            const originalOpen = xhr.open;

            xhr.open = function(method, url) {
                window.__apiUrls.add(url);
                return originalOpen.apply(this, arguments);
            };

            return xhr;
        };

        // 5秒后返回结果
        setTimeout(() => {
            resolve(Array.from(window.__apiUrls));
        }, 5000);
    });
"""

get_urls = """
return window.__capturedUrls ? Array.from(window.__capturedUrls) : [];
"""


# 改进的拦截脚本，直接返回URL
intercept_script_improved = """
// 初始化数组
if (!window.__capturedUrls) {
    window.__capturedUrls = [];
}

// 拦截 fetch API
const originalFetch = window.fetch;
window.fetch = function(...args) {
    const [url, options = {}] = args;
    const method = options.method || 'GET';

    return originalFetch.apply(this, args).then(response => {
        window.__capturedUrls.push({
            url: url,
            method: method,
            status: response.status,
            type: 'fetch',
            timestamp: new Date().toISOString()
        });
        return response;
    });
};

// 拦截 XMLHttpRequest
const originalXHR = window.XMLHttpRequest;
window.XMLHttpRequest = function() {
    const xhr = new originalXHR();
    const originalOpen = xhr.open;
    const originalSend = xhr.send;

    let requestUrl = '';
    let requestMethod = '';

    xhr.open = function(method, url) {
        requestMethod = method;
        requestUrl = url;
        return originalOpen.apply(this, arguments);
    };

    xhr.send = function(body) {
        this.addEventListener('load', function() {
            window.__capturedUrls.push({
                url: requestUrl,
                method: requestMethod,
                status: this.status,
                type: 'xhr',
                timestamp: new Date().toISOString()
            });
        });

        return originalSend.apply(this, arguments);
    };

    return xhr;
};

// 返回当前已捕获的URL数量
return `拦截器已设置，当前有 ${window.__capturedUrls.length} 个URL`;
"""

# 查询函数
get_urls_script = """
return window.__capturedUrls ? window.__capturedUrls.map(r => r.url) : [];
"""